$(function () {

    $("#content").keyup(function () {
        var len = $(this).val().length;
        if (len > 299) {
            $(this).val($(this).val().substring(0, 300));
        }
        $("#word").text(len);
    });
    $("#admincontent").keyup(function () {
        var len = $(this).val().length;
        if (len > 299) {
            $(this).val($(this).val().substring(0, 300));
        }
        $("#adminword").text(len);
    });
    // 初始请求页面
    init()



    // 删除
    $(document).on("click", '.btn-danger', function(){
        // 获取tr层 方法1：
        var tr = $(this).closest("tr")
        var id = tr.find('.id').text()
        const index = Number(id)
        // const index = parseFloat(id)
        console.log(typeof index )
        $(".delebtn").click(function () {
            console.log("删除")
            $.ajax({
                // url: '/userManage/delete',
                // data:"id="+index,
                url: '/userManage/delete?id='+id,
                type: 'Get',
                dataType: "json",
                // async: true,
                processData: false,   // 不处理发送的数据
                contentType: false,   // 不设置Content-Type请求头
                success: function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        $.toast({
                            title: '通知',
                            subtitle: '5秒前',
                            content: "删除成功",
                            type: 'success',
                            delay: 3000,
                            pause_on_hover: false
                        });
                        $(this).parent().parent().parent().remove()
                        // $('#delmodal').modal('hide')
                    } else {
                        $.toast({
                            title: '通知',
                            subtitle: '5秒前',
                            content: res.msg,
                            type: 'error',
                            delay: 3000,
                            pause_on_hover: false
                        });
                    }
                }
            })
        })
        // 方法2




        // console.log($(this).parent().parent().parent().eq(0).text())
        // console.log($(this).parent().parent())
        // console.log($(this).parent().parent().text())
        // console.log($(this).parent().parent().eq(0).text())
        //
        // console.log(index)


    })
    // 修改
    $(document).on('click','.btn-info',function () {
        let tr = $(this).closest("tr")
        let id = tr.find('.id').text()
        let username = tr.find('.username').text()
        let role = tr.find('.role').text()
        let email = tr.find('.email').text()
        let school = tr.find('.school').text()
        let education = tr.find('.education').text()
        let content = tr.find('.content').text()
        $('.userId').val(id)
        $('.username').val(username)
        $('.userrole').val(role=='管理员'?'1':'2')
        $('.useremail').val(email)
        $('.school').val(school)
        $('.education').val(education)
        $('.content').val(content)


        let UserManageUpdateVO = new FormData()

        $('.editbtn').on('click',function () {
            $('.editbtn').attr("disabled", "disabled");
            UserManageUpdateVO.append('id', $('.userId').val())
            UserManageUpdateVO.append('username', $('#username').val())
            UserManageUpdateVO.append('roleId',  $('.userrole').val())
            UserManageUpdateVO.append('school', $('#school').val())
            UserManageUpdateVO.append('education', $('#education').val())
            UserManageUpdateVO.append('content', $('#content').val())
            $.ajax({
                    url: '/userManage/update',
                    type: 'POST',
                    dataType: "json",
                    data:UserManageUpdateVO,
                    // async: true,
                    processData: false,   // 不处理发送的数据
                    contentType: false,   // 不设置Content-Type请求头
                    success:
                        function (res) {
                            console.log(res)
                            // $('#editmodal').modal('toggle');
                            // $('.editbtn').removeAttr("disabled");

                            if (res.code == 200) {
                                $.toast({
                                    title: '通知',
                                    subtitle: '1秒前',
                                    content: "修改成功",
                                    type: 'success',
                                    delay: 1000,
                                    pause_on_hover: false
                                });
                                // init()
                                window.location.href='/userManage/index'
                            } else {
                                $.toast({
                                    title: '通知',
                                    subtitle: '1秒前',
                                    content: res.msg,
                                    type: 'error',
                                    delay: 30000,
                                    pause_on_hover: false
                                });
                            }
                        }
                }
            )

        })


    })

    // 增加
    $('.adddataForm').off().submit(function () {
        var UserManageCreateVO=$('.adddataForm').serialize();
        console.log(UserManageCreateVO)
        $.ajax({
                url: '/userManage/insert?'+UserManageCreateVO,
                type: 'POST',
                dataType: "json",
                // data:UserManageCreateVO,
                // data: ,
                // async: true,
                processData: false,   // 不处理发送的数据
                contentType: false,   // 不设置Content-Type请求头
                success:
                    function (res) {
                        console.log(res)
                        if (res.code == 200) {
                            $.toast({
                                title: '通知',
                                subtitle: '5秒前',
                                content: res.msg,
                                type: 'success',
                                delay: 1000,
                                pause_on_hover: false
                            });
                            // window.location.href = '/userManage/index';
                        } else {
                            $.toast({
                                title: '通知',
                                subtitle: '5秒前',
                                content: res.msg,
                                type: 'error',
                                delay: 1000,
                                pause_on_hover: false
                            });
                        }
                    }
            }
        )
        return false;
    })


})



function xianshi(num,page,data) {
    $('.listbody').children().remove()
    var page2 = (page-1)*10
    for (let i = 0; i < num; i++) {
        if(data[i+page2]){
            var $str = $('<tr>' +
                '<td class="id">'+data[i+page2].id+'</td>' +
                '<td class="username">'+data[i+page2].username+'</td>' +
                '<td class="role">'+data[i+page2].roleId+'</td>' +
                '<td class="email">'+data[i+page2].email+'</td>' +
                '<td>' +
                '<div class="operation-btn">\n' +
                '<button type="button" class="btn btn-info" data-toggle="modal" data-target="#editmodal"><i class="fas fa-edit"></i> 修改</button>\n' +
                '<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#delmodal"><i class="fas fa-eraser"></i> 删除</button>\n' +
                '</div>' +
                '</td>' +
                '<td class="education" style="display: none">'+data[i+page2].education+'</td>' +
                '<td class="school" style="display: none">'+data[i+page2].school+'</td>' +
                '<td class="content" style="display: none">'+data[i+page2].content+'</td>' +
                '</tr>')
            $('.listbody').append($str)
        }
    }
    $('.role').each(function () {
        if($(this).text()==1){
            $(this).text('管理员')
        }
        else{
            $(this).text('普通用户')
        }

    })
}

function init(){
    $.ajax({
        url: '/userManage/list',
        type: 'Get',
        dataType: "json",
        // data: ,
        // async: true,
        processData: false,   // 不处理发送的数据
        contentType: false,   // 不设置Content-Type请求头
        success:
            function (res) {
                if (res.code!==200){
                    $.toast({
                        title: '通知',
                        subtitle: '1秒前',
                        content: res.msg,
                        type: 'success',
                        delay: 30000,
                        pause_on_hover: false
                    });
                    return;
                }
                var data  = res.data
                console.log(data)
                xianshi(10,1,data)
                var page = Math.ceil(data.length/10)
                $('#pagination').pagination({
                    pageCount: page,
                    jump: true,
                    coping: true,
                    homePage: '首页',
                    endPage: '末页',
                    prevContent: '上页',
                    nextContent: '下页',
                    current:1,  //当前第几页
                    callback:function (api) {
                        console.log(api.getCurrent())
                        xianshi(10,api.getCurrent(),data)
                    }
                })
            }
    })

}